package io.itit.ecp.admin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.server.entity.DeviceRunningValueEntity;
import io.itit.ecp.admin.server.mapper.DeviceRunningValueMapper;
import io.itit.ecp.admin.server.service.IDeviceRunningValueService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


@Service
@RequiredArgsConstructor
public class DeviceRunningValueServiceImpl extends JoinServiceImpl<DeviceRunningValueMapper, DeviceRunningValueEntity>
        implements IDeviceRunningValueService {

    @Override
    public String getValue(String deviceId, String physicalModelId) {
        return getOneOpt(new LambdaQueryWrapper<DeviceRunningValueEntity>()
                .eq(DeviceRunningValueEntity::getDeviceId, deviceId)
                .eq(DeviceRunningValueEntity::getPhysicalModelId, physicalModelId)
        ).map(DeviceRunningValueEntity::getRunningValue).orElse(null);
    }

    @Override
    public boolean existsValueByDeviceIdAndPhysicalModelId(String deviceId, String physicalModelId) {
        return exists(new LambdaQueryWrapper<DeviceRunningValueEntity>()
                .eq(DeviceRunningValueEntity::getDeviceId, deviceId)
                .eq(DeviceRunningValueEntity::getPhysicalModelId, physicalModelId));
    }

    @Override
    public Optional<DeviceRunningValueEntity> findByDeviceIdAndPhysicalModelId(String deviceId, String physicalModelId) {
        return getOneOpt(new LambdaQueryWrapper<DeviceRunningValueEntity>()
                .eq(DeviceRunningValueEntity::getDeviceId, deviceId)
                .eq(DeviceRunningValueEntity::getPhysicalModelId, physicalModelId)
        );
    }

    @Override
    public List<DeviceRunningValueEntity> findByDeviceId(String deviceId) {
        return lambdaQuery()
                .eq(DeviceRunningValueEntity::getDeviceId, deviceId)
                .list();
    }

    @Override
    public void removeByDeviceIdAndPhysicalModelId(String deviceId, String physicalModelId) {
        remove(new LambdaQueryWrapper<DeviceRunningValueEntity>()
                .eq(DeviceRunningValueEntity::getDeviceId, deviceId)
                .eq(DeviceRunningValueEntity::getPhysicalModelId, physicalModelId));
    }
}
